// Created by carolsail

import { ENUM_GAME_MODE, ENUM_GAME_STATUS } from '../Enum';
import { BaseWsClient } from 'tsrpc-base-client';
import { ServiceType } from '../shared/protocols/serviceProto';
import { IRoom, IUser } from '../shared/common/types';

const STORAGE_KEY = 'FLYCHESS_ONLINE'

// 出生地
export const POINT_BORN = [
    [{ x: -302.509, y: -247.114, rotation: 0 }, { x: -246.477, y: -247.114, rotation: 0 }, { x: -302.509, y: -302.632, rotation: 0 }, { x: -246.477, y: -302.632, rotation: 0 }],
    [{ x: -302.509, y: 304.514, rotation: -90 }, { x: -246.477, y: 304.514, rotation: -90 }, { x: -302.509, y: 248.482, rotation: -90 }, { x: -246.477, y: 248.482, rotation: -90 }],
    [{ x: 246.709, y: 304.514, rotation: -180 }, { x: 302.633, y: 304.514, rotation: -180 }, { x: 246.709, y: 248.59, rotation: -180 }, { x: 302.633, y: 248.59, rotation: -180 }],
    [{ x: 246.709, y: -247.114, rotation: -270 }, { x: 302.633, y: -247.114, rotation: -270 }, { x: 246.709, y: -302.632, rotation: -270 }, { x: 302.633, y: -302.632, rotation: -270 }]
]
// 起飞地
export const POINT_START = [
    { x: -180, y: -322, rotation: 0 },
    { x: -328, y: 181, rotation: -90 },
    { x: 180, y: 329, rotation: -180 },
    { x: 326, y: -182, rotation: -270 }
]

// 环绕跑道
export const POINT_ROUND = [
    [
        { x: -143.778, y: -307.597, rotation: 0 },
        { x: -153.562, y: -260.456, rotation: 0, jumpto: 5 },
        { x: -153.562, y: -212.074, rotation: 0 },
        { x: -143.637, y: -164.932, rotation: 0 },
        { x: -170.93, y: -148.805, rotation: -270 },
        { x: -210.628, y: -155.008, rotation: -270, jumpto: 9 },
        { x: -261.491, y: -155.008, rotation: -270 },
        { x: -311.114, y: -148.805, rotation: 0 },
        { x: -316.366, y: -97.333, rotation: 0 },
        { x: -316.366, y: -49.013, rotation: 0, jumpto: 13 },
        { x: -316.366, y: 0, rotation: 0 },
        { x: -316.366, y: 48.92, rotation: 0 },
        { x: -316.366, y: 96.951, rotation: 0 },
        { x: -308.951, y: 147.699, rotation: -90, jumpto: 17 },
        { x: -260.018, y: 147.699, rotation: -90 },
        { x: -209.84, y: 147.699, rotation: -90 },
        { x: -163.123, y: 147.699, rotation: -90 },
        { x: -145.82, y: 171.923, rotation: 0, jumpto: 29 },
        { x: -154.471, y: 213.45, rotation: 0 },
        { x: -154.471, y: 260.167, rotation: 0 },
        { x: -154.471, y: 310.345, rotation: -90 },
        { x: -99.102, y: 318.996, rotation: -90, jumpto: 25 },
        { x: -48.924, y: 318.996, rotation: -90 },
        { x: 0, y: 318.996, rotation: -90 },
        { x: 46.717, y: 318.996, rotation: -90 },
        { x: 93.434, y: 318.996, rotation: -90, jumpto: 29 },
        { x: 147.072, y: 309.565, rotation: -180 },
        { x: 155.723, y: 260.167, rotation: -180 },
        { x: 155.723, y: 213.168, rotation: -180 },
        { x: 143.462, y: 164.125, rotation: -90, jumpto: 33 },
        { x: 165.94, y: 149.821, rotation: -90 },
        { x: 208.852, y: 149.821, rotation: -90 },
        { x: 259.938, y: 149.821, rotation: -90 },
        { x: 315.111, y: 149.821, rotation: -180, jumpto: 37 },
        { x: 315.111, y: 100.778, rotation: -180 },
        { x: 315.111, y: 49.692, rotation: -180 },
        { x: 315.111, y: 0, rotation: -180 },
        { x: 315.111, y: -48.448, rotation: -180, jumpto: 41 },
        { x: 315.111, y: -95.165, rotation: -180 },
        { x: 306.46, y: -145.062, rotation: -270 },
        { x: 261.473, y: -155.725, rotation: -270 },
        { x: 209.565, y: -155.725, rotation: -270, jumpto: 45 },
        { x: 162.848, y: -148.804, rotation: -270 },
        { x: 138.747, y: -166.126, rotation: -180 },
        { x: 154.504, y: -208.143, rotation: -180 },
        { x: 154.504, y: -259.422, rotation: -180, jumpto: 49 },
        { x: 148.644, y: -315.096, rotation: -270 },
        { x: 97.365, y: -315.096, rotation: -270 },
        { x: 50.482, y: -315.096, rotation: -270 },
        { x: 0, y: -315.096, rotation: 0 },
        // { x: -47.681, y: -315.096, rotation: -270 },
        // { x: -97.495, y: -315.096, rotation: 0 }
    ],
    [
        { x: -308.951, y: 147.699, rotation: -90 },
        { x: -260.018, y: 147.699, rotation: -90, jumpto: 5 },
        { x: -209.84, y: 147.699, rotation: -90 },
        { x: -163.123, y: 147.699, rotation: -90 },
        { x: -145.82, y: 171.923, rotation: 0 },
        { x: -154.471, y: 213.45, rotation: 0, jumpto: 9 },
        { x: -154.471, y: 260.167, rotation: 0 },
        { x: -154.471, y: 310.345, rotation: -90 },
        { x: -99.102, y: 318.996, rotation: -90 },
        { x: -48.924, y: 318.996, rotation: -90, jumpto: 13 },
        { x: 0, y: 318.996, rotation: -90 },
        { x: 46.717, y: 318.996, rotation: -90 },
        { x: 93.434, y: 318.996, rotation: -90 },
        { x: 147.072, y: 309.565, rotation: -180, jumpto: 17 },
        { x: 155.723, y: 260.167, rotation: -180 },
        { x: 155.723, y: 213.168, rotation: -180 },
        { x: 143.462, y: 164.125, rotation: -180 },
        { x: 165.94, y: 149.821, rotation: -90, jumpto: 29 },
        { x: 208.852, y: 149.821, rotation: -90 },
        { x: 259.938, y: 149.821, rotation: -90 },
        { x: 315.111, y: 149.821, rotation: -180 },
        { x: 315.111, y: 100.778, rotation: -180, jumpto: 25 },
        { x: 315.111, y: 49.692, rotation: -180 },
        { x: 315.111, y: 0, rotation: -180 },
        { x: 315.111, y: -48.448, rotation: -180 },
        { x: 315.111, y: -95.165, rotation: -180, jumpto: 29 },
        { x: 306.46, y: -145.062, rotation: -270 },
        { x: 261.473, y: -155.725, rotation: -270 },
        { x: 209.565, y: -155.725, rotation: -270 },
        { x: 162.848, y: -148.804, rotation: -180, jumpto: 33 },
        { x: 138.747, y: -166.126, rotation: -180 },
        { x: 154.504, y: -208.143, rotation: -180 },
        { x: 154.504, y: -259.422, rotation: -180 },
        { x: 148.644, y: -315.096, rotation: -270, jumpto: 37 },
        { x: 97.365, y: -315.096, rotation: -270 },
        { x: 50.482, y: -315.096, rotation: -270 },
        { x: 0, y: -315.096, rotation: -270 },
        { x: -47.681, y: -315.096, rotation: -270, jumpto: 41 },
        { x: -97.495, y: -315.096, rotation: -270 },
        { x: -143.778, y: -307.597, rotation: 0 },
        { x: -153.562, y: -260.456, rotation: 0 },
        { x: -153.562, y: -212.074, rotation: 0, jumpto: 45 },
        { x: -143.637, y: -164.932, rotation: 0 },
        { x: -170.93, y: -148.805, rotation: -270 },
        { x: -210.628, y: -155.008, rotation: -270 },
        { x: -261.491, y: -155.008, rotation: -270, jumpto: 49 },
        { x: -311.114, y: -148.805, rotation: 0 },
        { x: -316.366, y: -97.333, rotation: 0 },
        { x: -316.366, y: -49.013, rotation: 0 },
        { x: -316.366, y: 0, rotation: -90 },
    ],
    [
        { x: 147.072, y: 309.565, rotation: -180 },
        { x: 155.723, y: 260.167, rotation: -180, jumpto: 5 },
        { x: 155.723, y: 213.168, rotation: -180 },
        { x: 143.462, y: 164.125, rotation: -180 },
        { x: 165.94, y: 149.821, rotation: -90 },
        { x: 208.852, y: 149.821, rotation: -90, jumpto: 9 },
        { x: 259.938, y: 149.821, rotation: -90 },
        { x: 315.111, y: 149.821, rotation: -180 },
        { x: 315.111, y: 100.778, rotation: -180 },
        { x: 315.111, y: 49.692, rotation: -180, jumpto: 13 },
        { x: 315.111, y: 0, rotation: -180 },
        { x: 315.111, y: -48.448, rotation: -180 },
        { x: 315.111, y: -95.165, rotation: -180 },
        { x: 306.46, y: -145.062, rotation: -270, jumpto: 17 },
        { x: 261.473, y: -155.725, rotation: -270 },
        { x: 209.565, y: -155.725, rotation: -270 },
        { x: 162.848, y: -148.804, rotation: -270 },
        { x: 138.747, y: -166.126, rotation: -180, jumpto: 29 },
        { x: 154.504, y: -208.143, rotation: -180 },
        { x: 154.504, y: -259.422, rotation: -180 },
        { x: 148.644, y: -315.096, rotation: -270 },
        { x: 97.365, y: -315.096, rotation: -270, jumpto: 25 },
        { x: 50.482, y: -315.096, rotation: -270 },
        { x: 0, y: -315.096, rotation: -270 },
        { x: -47.681, y: -315.096, rotation: -270 },
        { x: -97.495, y: -315.096, rotation: -270, jumpto: 29 },
        { x: -143.778, y: -307.597, rotation: 0 },
        { x: -153.562, y: -260.456, rotation: 0 },
        { x: -153.562, y: -212.074, rotation: 0 },
        { x: -143.637, y: -164.932, rotation: -270, jumpto: 33 },
        { x: -170.93, y: -148.805, rotation: -270 },
        { x: -210.628, y: -155.008, rotation: -270 },
        { x: -261.491, y: -155.008, rotation: -270 },
        { x: -311.114, y: -148.805, rotation: 0, jumpto: 37 },
        { x: -316.366, y: -97.333, rotation: 0 },
        { x: -316.366, y: -49.013, rotation: 0 },
        { x: -316.366, y: 0, rotation: 0 },
        { x: -316.366, y: 48.92, rotation: 0, jumpto: 41 },
        { x: -316.366, y: 96.951, rotation: 0 },
        { x: -308.951, y: 147.699, rotation: -90 },
        { x: -260.018, y: 147.699, rotation: -90 },
        { x: -209.84, y: 147.699, rotation: -90, jumpto: 45 },
        { x: -163.123, y: 147.699, rotation: -90 },
        { x: -145.82, y: 171.923, rotation: 0 },
        { x: -154.471, y: 213.45, rotation: 0 },
        { x: -154.471, y: 260.167, rotation: 0, jumpto: 49 },
        { x: -154.471, y: 310.345, rotation: -90 },
        { x: -99.102, y: 318.996, rotation: -90 },
        { x: -48.924, y: 318.996, rotation: -90 },
        { x: 0, y: 318.996, rotation: -180 }
    ],
    [
        { x: 306.46, y: -145.062, rotation: -270 },
        { x: 261.473, y: -155.725, rotation: -270, jumpto: 5 },
        { x: 209.565, y: -155.725, rotation: -270 },
        { x: 162.848, y: -148.804, rotation: -270 },
        { x: 138.747, y: -166.126, rotation: -180 },
        { x: 154.504, y: -208.143, rotation: -180, jumpto: 9 },
        { x: 154.504, y: -259.422, rotation: -180 },
        { x: 148.644, y: -315.096, rotation: -270 },
        { x: 97.365, y: -315.096, rotation: -270 },
        { x: 50.482, y: -315.096, rotation: -270, jumpto: 13 },
        { x: 0, y: -315.096, rotation: -270 },
        { x: -47.681, y: -315.096, rotation: -270 },
        { x: -97.495, y: -315.096, rotation: -270 },
        { x: -143.778, y: -307.597, rotation: 0, jumpto: 17 },
        { x: -153.562, y: -260.456, rotation: 0 },
        { x: -153.562, y: -212.074, rotation: 0 },
        { x: -143.637, y: -164.932, rotation: 0 },
        { x: -170.93, y: -148.805, rotation: -270, jumpto: 29 },
        { x: -210.628, y: -155.008, rotation: -270 },
        { x: -261.491, y: -155.008, rotation: -270 },
        { x: -311.114, y: -148.805, rotation: 0 },
        { x: -316.366, y: -97.333, rotation: 0, jumpto: 25 },
        { x: -316.366, y: -49.013, rotation: 0 },
        { x: -316.366, y: 0, rotation: 0 },
        { x: -316.366, y: 48.92, rotation: 0 },
        { x: -316.366, y: 96.951, rotation: 0, jumpto: 29 },
        { x: -308.951, y: 147.699, rotation: -90 },
        { x: -260.018, y: 147.699, rotation: -90 },
        { x: -209.84, y: 147.699, rotation: -90 },
        { x: -163.123, y: 147.699, rotation: 0, jumpto: 33 },
        { x: -145.82, y: 171.923, rotation: 0 },
        { x: -154.471, y: 213.45, rotation: 0 },
        { x: -154.471, y: 260.167, rotation: 0 },
        { x: -154.471, y: 310.345, rotation: -90, jumpto: 37 },
        { x: -99.102, y: 318.996, rotation: -90 },
        { x: -48.924, y: 318.996, rotation: -90 },
        { x: 0, y: 318.996, rotation: -90 },
        { x: 46.717, y: 318.996, rotation: -90, jumpto: 41 },
        { x: 93.434, y: 318.996, rotation: -90 },
        { x: 147.072, y: 309.565, rotation: -180 },
        { x: 155.723, y: 260.167, rotation: -180 },
        { x: 155.723, y: 213.168, rotation: -180, jumpto: 45 },
        { x: 143.462, y: 164.125, rotation: -180 },
        { x: 165.94, y: 149.821, rotation: -90 },
        { x: 208.852, y: 149.821, rotation: -90 },
        { x: 259.938, y: 149.821, rotation: -90, jumpto: 49 },
        { x: 315.111, y: 149.821, rotation: -180 },
        { x: 315.111, y: 100.778, rotation: -180 },
        { x: 315.111, y: 49.692, rotation: -180 },
        { x: 315.111, y: 0, rotation: -270 }
    ]
]

// 直线跑道
export const POINT_LINE = [
    [
        { x: 0, y: -262.493, rotation: 0 },
        { x: 0, y: -217.075, rotation: 0 },
        { x: 0, y: -174.587, rotation: 0 },
        { x: 0, y: -127.704, rotation: 0 },
        { x: 0, y: -82.286, rotation: 0 },
        { x: 0, y: -38.333, rotation: 0 }
    ],
    [
        { x: -264.552, y: 0, rotation: -90 },
        { x: -214.374, y: 0, rotation: -90 },
        { x: -172.847, y: 0, rotation: -90 },
        { x: -129.59, y: 0, rotation: -90 },
        { x: -88.063, y: 0, rotation: -90 },
        { x: -39.615, y: 0, rotation: -90 }
    ],
    [
        { x: 0, y: 262.493, rotation: -180 },
        { x: 0, y: 217.075, rotation: -180 },
        { x: 0, y: 174.587, rotation: -180 },
        { x: 0, y: 127.704, rotation: -180 },
        { x: 0, y: 82.286, rotation: -180 },
        { x: 0, y: 38.333, rotation: -180 }
    ],
    [
        { x: 264.552, y: 0, rotation: -270 },
        { x: 214.374, y: 0, rotation: -270 },
        { x: 172.847, y: 0, rotation: -270 },
        { x: 129.59, y: 0, rotation: -270 },
        { x: 88.063, y: 0, rotation: -270 },
        { x: 39.615, y: 0, rotation: -270 }
    ]
]

export default class DataManager {

    private static _instance: any = null

    static getInstance<T>(): T {
        if (this._instance === null) {
            this._instance = new this()
        }

        return this._instance
    }

    static get instance() {
        return this.getInstance<DataManager>()
    }

    // 游戏模式
    mode: ENUM_GAME_MODE = ENUM_GAME_MODE.SINGLE
    // 游戏状态
    status: ENUM_GAME_STATUS = ENUM_GAME_STATUS.UNRUNING
    // 加载进度
    loadingRate: number = 0
    // 声音开启
    _isMusicOn: boolean = true
    _isSoundOn: boolean = true
    // 更多游戏
    games: any[] = [
        { title: '消灭星星', icon: 'xiao2d', appid: '', url: 'https://store.cocos.com/app/detail/4183' },
        { title: '实况足球杯', icon: 'football', appid: '', url: 'https://store.cocos.com/app/detail/4221' },
        { title: '爬了个爬', icon: 'stairway', appid: '', url: 'https://store.cocos.com/app/detail/4314' },
        { title: '咩了个咩3D', icon: 'xiao3d', appid: '', url: 'https://store.cocos.com/app/detail/4148' },
        { title: '经典泡泡龙', icon: 'bubble', appid: '', url: 'https://store.cocos.com/app/detail/4370' },
    ]
    // 背景
    bgIndex: number = 0
    bgs: any[] = [
        { index: 0, title: '默认', icon: 'bg_game', isAd: false },
        { index: 1, title: '天空', icon: 'bg_game2', isAd: true },
        { index: 2, title: '森林', icon: 'bg_game3', isAd: true },
        { index: 3, title: '可爱', icon: 'bg_game4', isAd: true },
        { index: 4, title: '科技', icon: 'bg_game5', isAd: true }
    ]

    // 身份 (红，黄，蓝，绿)
    role: number = 0
    roleString: string[] = ['红方', '黄方', '蓝方', '绿方']
    // 回合 (红，黄，蓝，绿)
    round: number = 0
    // 是否为ai (红，黄，蓝，绿)
    isAi: boolean[] = [false, true, true, true]
    // 是否ai补位
    isAiCover: boolean = false
    // 位置数据 (红，黄，蓝，绿)
    posData: number[][] = [
        [-2, -2, -2, -2],
        [-2, -2, -2, -2],
        [-2, -2, -2, -2],
        [-2, -2, -2, -2]
    ]
    // 起飞情况
    canFlyIndex: number = 0
    canFlyNum: number[][] = [
        [6],
        [5, 6],
        [2, 4, 6]
    ]
    // 击杀数
    killData: number[] = [0, 0, 0, 0]
    // 筛子数
    diceIndex: number = 0

    // ws客户端
    wsClient: BaseWsClient<ServiceType> = null
    // 用户信息
    user: IUser = null
    // 房间信息
    room: IRoom = null
    // 上一次被邀请时间
    lastSharedTime: number = null
    // 邀请房间号
    shareRoomId: string = null
    // 联机再玩一次同意记录
    restartCount: number = 0

    get isMusicOn() {
        return this._isMusicOn
    }

    set isMusicOn(data: boolean) {
        this._isMusicOn = data
    }

    get isSoundOn() {
        return this._isSoundOn
    }

    set isSoundOn(data: boolean) {
        this._isSoundOn = data
    }

    reset() {
        this.status = ENUM_GAME_STATUS.UNRUNING
        this.killData = [0, 0, 0, 0]
        this.diceIndex = 0
        this.restartCount = 0
    }

    save() {
        cc.sys.localStorage.setItem(STORAGE_KEY, JSON.stringify({
            isSoundOn: this.isSoundOn,
            isMusicOn: this.isMusicOn,
            bgIndex: this.bgIndex,
            user: this.user
        }))
    }

    restore() {
        const _data = cc.sys.localStorage.getItem(STORAGE_KEY) as any
        try {
            const data = JSON.parse(_data)
            this.isMusicOn = data?.isMusicOn === false ? false : true
            this.isSoundOn = data?.isSoundOn === false ? false : true
            this.bgIndex = data?.bgIndex || 0
            this.user = data?.user || null
        } catch {
            this.isMusicOn = true
            this.isSoundOn = true
            this.bgIndex = 0
            this.user = null
        }
    }
}
